# -*- coding: utf-8 -*-

from odoo import models, fields, api
from dateutil.relativedelta import relativedelta

class Acc(models.Model):
    _name='bank_acc.acc'
    active = fields.Boolean(default=True)
    currency_id = fields.Many2one('res.currency', string='币种')
    bank_id=fields.Many2one('res.partner.bank','银行')
    amt=fields.Monetary('金额',currency_field = 'currency_id',default=0)
    period=fields.Integer('存期(月)',default=12)
    date_start=fields.Date('存入日期',default=fields.Date.today())
    date_end=fields.Date('到期日',compute='_compute_end',store=True,readonly=False)
    rate=fields.Float('年利率(%)',digits=(5,4),default=0)
    int=fields.Float('利息',digits=(10,2),compute='_compute_int',store=True,readonly=False)
    total=fields.Monetary('本息合计',currency_field='currency_id',compute='_compute_total')
    note=fields.Char('备注')
    card_holder=fields.Selection('持卡人',related='bank_id.card_holder',store=True)
    paper_account=fields.Char("存单账号")

    @api.depends('period','date_start')
    def _compute_end(self):
        for rec in self:
            if self.date_start:
                rec.date_end=self.date_start+relativedelta(months=self.period)

    @api.depends('period', 'amt','rate')
    def _compute_int(self):
        for rec in self:
            rec.int=rec.amt*rec.rate/100/12*rec.period

    @api.depends('amt', 'int')
    def _compute_total(self):
        for rec in self:
            rec.total=rec.amt+rec.int

class ResPartnerBank(models.Model):
    _inherit='res.partner.bank'
    _rec_name='bank_name'
    sn = [
        ('数字6', '数字6'),
        ('数字8', '数字8'),
        ('字母数字', '字母数字'),
        ('数字字母', '数字字母'),
    ]
    user = [
        ('UKEY','UKEY'),
        ('卡号', '卡号'),
        ('手机', '手机'),
        ('dibohao1', 'dibohao1'),
        ('dibohao', 'dibohao'),
        ('DI BOHAO', 'DI BOHAO'),
        ('数字字母', '数字字母'),
        ('身份证号', '身份证号'),
        ('paul+数字', 'paul+数字'),
    ]
    partner_id = fields.Many2one('res.partner', 'Account Holder', ondelete='cascade', index=True,
                                 domain=['|', ('is_company', '=', True), ('parent_id', '=', False)], required=False)
    pc_user=fields.Selection(user,'PC登录用户名')
    mobile_user=fields.Selection(user,'手机登录用户名')
    pc_pin=fields.Selection(sn,'PC登录密码')
    mobile_sn=fields.Selection(sn,'手机登录密码')
    # bank_en=fields.Char('Bank Name')
    card_holder=fields.Selection([('dbh','dbh'),('zam','zam'),('dxj','dxj'),('slj','slj'),('ml','ml')],'卡持有人')
    remark=fields.Char('备注')
    def name_get(self):
        result = []
        for record in self:
            result.append(
                (record.id,
                 u"%s(%s)" % (record.bank_name,record.acc_number, )
                 ))
        return result

class Bank(models.Model):
    _description = 'Bank'
    _inherit = 'res.bank'
    bank_en = fields.Char('Bank Name')

class BankAccJournal(models.Model):
    _description = '事物记录'
    _name = 'bank.acc.jouranl'
   
    name=fields.Char('标题')
    journal_line_ids=fields.One2many('bank.acc.jouranl.lines','journal_id',string='日记账明细')
    document_datas = fields.Binary(string='附件', attachment=True)
    document_fname = fields.Char('附件名', index=True)
    document_datas2 = fields.Binary(string='附件2', attachment=True)
    document_fname2 = fields.Char('附件名2', index=True)
    document_datas3 = fields.Binary(string='附件3', attachment=True)
    document_fname3 = fields.Char('附件名3', index=True)
    document_datas4 = fields.Binary(string='附件4', attachment=True)
    document_fname4 = fields.Char('附件名4', index=True)
    currency_id = fields.Many2one('res.currency', string='币种',default=lambda self:self.env.ref('base.CNY'))
    total = fields.Monetary('合计',currency_field = 'currency_id',compute='_compute_total')

    @api.depends('journal_line_ids')
    def _compute_total(self):
        for l in self:
            l.total=sum(l.mapped('journal_line_ids.input'))
      

class BankAccJournalLines(models.Model):
    _description = '事物记录明细'
    _name = 'bank.acc.jouranl.lines'

    journal_id=fields.Many2one('bank.acc.jouranl',string='日记账')
    current_date=fields.Date('日期',default=fields.Date.today())
    name=fields.Char('内容')
    currency_id = fields.Many2one('res.currency', string='币种',default=lambda self:self.env.ref('base.CNY'))
    input=fields.Monetary('收入',currency_field = 'currency_id',default=0)
    # output=fields.Monetary('支出',currency_field = 'currency_id',default=0)
    # balance=fields.Monetary('余额',currency_field = 'currency_id',default=0,compute='_compute_balance')

    # @api.depends('input','output')
    # def _compute_balance(self):
    #     balance=0
    #     for j in self:
    #         balance=balance+j.input-j.output
    #     self.balance=balance
            
    

    
    currency_id = fields.Many2one('res.currency', string='币种',default=lambda self:self.env.ref('base.CNY'))
    total = fields.Monetary('合计',currency_field = 'currency_id',compute='_compute_total')

    @api.depends('journal_line_ids')
    def _compute_total(self):
        for l in self:
            l.total=sum(l.mapped('journal_line_ids.input'))
      

class BankAccJournalLines(models.Model):
    _description = '事物记录明细'
    _name = 'bank.acc.jouranl.lines'

    journal_id=fields.Many2one('bank.acc.jouranl',string='日记账')
    current_date=fields.Date('日期',default=fields.Date.today())
    name=fields.Char('内容')
    currency_id = fields.Many2one('res.currency', string='币种',default=lambda self:self.env.ref('base.CNY'))
    input=fields.Monetary('收入',currency_field = 'currency_id',default=0)
    # output=fields.Monetary('支出',currency_field = 'currency_id',default=0)
    # balance=fields.Monetary('余额',currency_field = 'currency_id',default=0,compute='_compute_balance')

    # @api.depends('input','output')
    # def _compute_balance(self):
    #     balance=0
    #     for j in self:
    #         balance=balance+j.input-j.output
    #     self.balance=balance
            
    

    